<?php
abstract class Container extends Renderable {
	
	protected $elementArray = array();
	private $formElementArray = array();
	
	public function __construct($elementId) {
		
		parent::__construct($elementId);
	}
	
	public function addElement(Renderable $element) {
		
		$this->elementArray[] = $element;
		
		if ($element instanceof FormElement) {
			$this->formElementArray[] = $element;
		}
		
		$element->setParent($this);
	}
	
	protected function hasFormElements() {
		
		return count($this->formElementArray) == 0 ? TRUE : FALSE;
	}
	
	protected function getFormElements() {
		
		$outputArray = array();
		
		foreach($this->elementArray as $element) {
			
			if (!($element instanceof Container)) {
				continue;
			}
			
			$outputArray = array_merge($outputArray, $element->getFormElements());
		}
		
		return array_merge($outputArray, $this->formElementArray);
	}
	
	protected abstract function renderOpeningMarkup();
	
	protected abstract function renderClosingMarkup();
	
	protected function renderElement() {
		
		$this->renderOpeningMarkup();
		
		foreach ($this->elementArray as $element) {
			
			if ($element instanceof LabelledElement &&
				$element->getInlineLabel() == Label::$labelInline) {
				
				$labelContainer = new LabelControlContainer();
				$labelContainer->setParent($this);
				$labelContainer->addElement($element);
				$labelContainer->render();
				
			} else {
				$element->render();
			}
		}
		
		$this->renderClosingMarkup();
	}
}
?>